package com.nbot.server.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.log.StaticLog;
import com.nbot.server.domain.KeyParameterDO;
import com.nbot.server.domain.KeyParameterMonthDO;
import com.nbot.server.mapper.master.KeyParameterLogMapper;
import com.nbot.server.mapper.master.KeyParameterMapper;
import com.nbot.server.mapper.master.KeyParameterMonthMapper;
import com.nbot.server.service.KeyParameterMonthService;
import com.nbot.server.util.KeyParameterMonthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class KeyParameterMonthServiceImpl implements KeyParameterMonthService {

    @Autowired
    private KeyParameterMonthMapper keyParameterMonthMapper;
    @Autowired
    private KeyParameterMapper keyParameterMapper;
    @Autowired
    private KeyParameterLogMapper keyParameterLogMapper;

    @Override
    public KeyParameterMonthDO get(Long autoId) {
        return keyParameterMonthMapper.get(autoId);
    }

    @Override
    public List<KeyParameterMonthDO> list(Map<String, Object> map) {
        return keyParameterMonthMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return keyParameterMonthMapper.count(map);
    }

    @Override
    public int save(KeyParameterMonthDO keyParameterMonth) {
        return keyParameterMonthMapper.save(keyParameterMonth);
    }

    @Override
    public int update(KeyParameterMonthDO keyParameterMonth) {
        return keyParameterMonthMapper.update(keyParameterMonth);
    }

    @Override
    public int remove(Long autoId) {
        return keyParameterMonthMapper.remove(autoId);
    }

    @Override
    public int batchRemove(Long[] autoIds) {
        return keyParameterMonthMapper.batchRemove(autoIds);
    }

    @Override
    public void generateLog(String lastMonth) {
        String first = lastMonth + "-" + "01 00:00:00";
        Date sd = DateUtil.parse(first);

        DateTime sDate = DateUtil.beginOfMonth(sd);
        StaticLog.info(sDate.toString());
        DateTime eDate = DateUtil.endOfMonth(sd);
        StaticLog.info(eDate.toString());
        List<KeyParameterDO> kps = keyParameterMapper.list(null);
        if (kps == null) {
            return;
        }
        if (kps.isEmpty()) {
            return;
        }

        keyParameterMonthMapper.removeByYearMonth(lastMonth);
        for (KeyParameterDO kp : kps) {
            List kpLogs = keyParameterLogMapper.fingBetween(kp.getAutoId(), sDate.getTime() / 1000, eDate.getTime() / 1000);
            if (kpLogs == null) {
                continue;
            }
            if (kpLogs.isEmpty()) {
                continue;
            }


            KeyParameterMonthDO keyParameterMonthDO = new KeyParameterMonthDO();
            keyParameterMonthDO.setKpAutoId(kp.getAutoId());
            keyParameterMonthDO.setYearMonth(lastMonth);
            keyParameterMonthDO.setCount(kpLogs.size());
            BigDecimal sum = KeyParameterMonthUtil.getSum(kpLogs);
            keyParameterMonthDO.setSum(sum);
            keyParameterMonthDO.setAvg(sum.divide(new BigDecimal(kpLogs.size()), 4));
            keyParameterMonthDO.setMax(KeyParameterMonthUtil.getMax(kpLogs));
            keyParameterMonthDO.setMin(KeyParameterMonthUtil.getMin(kpLogs));
            keyParameterMonthDO.setCreateTime(new Date());

            keyParameterMonthMapper.save(keyParameterMonthDO);


        }


    }

}
